package pl.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.dojo.form.IAutocompleteModel;

public class AutoModel implements IAutocompleteModel {

	private List<Celebrity> celebrities;

	public AutoModel(List<Celebrity> celebrities) {

		this.celebrities = celebrities;
	}

	public String getLabelFor(Object obj) {

		Celebrity c = (Celebrity) obj;
		return c.getLastName();
	}

	public List getValues(String input) {

		List<Celebrity> list = new ArrayList<Celebrity>();
		for (Celebrity c : celebrities) {
			if (c.getLastName().toLowerCase().startsWith(input.toLowerCase())) {
				list.add(c);
			}
		}
		return list;
	}

	public Object getPrimaryKey(Object obj) {

		Celebrity c = (Celebrity) obj;
		return c.getId();
	}

	public Object getValue(Object id) {

		Integer i = (Integer) id;
		for (Celebrity c : celebrities) {
			if (c.getId() == i.intValue())
				return c;
		}
		return null;
	}
}
